package com.jcl.recruit.basic.register.impl;

import com.jcl.core.utils.DateUtils;
import com.jcl.recruit.basic.register.model.RegisterEnum;
import com.jcl.recruit.basic.register.model.RegisterResult;
import com.jcl.recruit.basic.register.vo.RegisterVo;
import com.jcl.recruit.console.ConsoleCorpDao;
import com.jcl.recruit.console.ConsoleDbCorpDao;
import com.jcl.recruit.console.mapper.ConsoleDbCorpMapper;
import com.jcl.recruit.console.ConsoleUserAccessDao;
import com.jcl.recruit.console.model.ConsoleCorpInfo;
import com.jcl.recruit.console.model.ConsoleDbCorp;
import com.jcl.recruit.console.model.ConsoleUserAccess;
import com.jcl.recruit.core.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName ConsoleRecruitRegister
 * @Author yyliu
 * @Date 2018/12/17 10:42
 * @Version 1.0
 **/
@Component
public class ConsoleRecruitRegister {

    @Autowired
    private ConsoleDbCorpDao consoleDbCorpDao;

    @Autowired
    private ConsoleCorpDao consoleCorpDao;

    @Autowired
    private ConsoleUserAccessDao consoleUserAccessDao;

    private RegisterVo registerVo;

    public RegisterResult register(String corpCode, RegisterEnum registerEnum) {
        RegisterResult result = null;
        ConsoleCorpInfo corpInfo = registerCorp(corpCode);
        if (null == corpInfo) {
            result = new RegisterResult();
            ConsoleUserAccess userAccess = null;
            if (registerEnum.equals(RegisterEnum.BASIC)) {
                userAccess = registerAccessUser(corpCode);
            }
            result.setUserAccess(userAccess);
            result.setConsoleCorpInfo(corpInfo);
        }
        return result;
    }


    public ConsoleCorpInfo registerCorp(String corpCode) {
        if (null != corpCode && null != registerVo) {
            ConsoleCorpInfo corpInfo = new ConsoleCorpInfo();
            corpInfo.setCode(corpCode);
            corpInfo.setEmail(registerVo.getEmail());
            corpInfo.setTelephone(registerVo.getTelephone());
            corpInfo.setFullName(registerVo.getCorpName());
            // 试用版
            corpInfo.setMealPackage(Constants.MealPackage.default_package);
            Date date = new Date();
            corpInfo.setAbateDate(DateUtils.addDays(date, Constants.MealPackage.default_day));
            corpInfo.setRegisterDate(date);
            corpInfo.setCreateTime(date);
            corpInfo.setUpdateTime(date);
            consoleCorpDao.insert(corpInfo);
            return corpInfo;
        }
        return null;
    }

    public ConsoleUserAccess registerAccessUser(String corpCode) {

        ConsoleUserAccess userAccess = new ConsoleUserAccess();
        return userAccess;
    }

    /* *
     *
     * 同步获取 corpCode
     * @Date 12:52 2018/12/14
     * @Author yyliu
     * @return java.lang.String
     **/
    public synchronized String getCorpCode() {
        ConsoleDbCorp entity = consoleDbCorpDao.getOne();
        if (null == entity) {
            // 需要抛出异常
            return null;
        }
        consoleDbCorpDao.updateStatus(ConsoleDbCorpMapper.STATUS_0, entity.getId());
        return entity.getCorpCode();
    }


    public RegisterVo getRegisterVo() {
        return registerVo;
    }

    public void setRegisterVo(RegisterVo registerVo) {
        this.registerVo = registerVo;
    }
}
